package com.itheima.service.umsService.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.db.ums.UmsAdmin;
import com.itheima.domain.db.ums.UmsAdminRoleRelation;
import com.itheima.domain.db.ums.UmsRole;
import com.itheima.mapper.umsMapper.UmsAdminMapper;
import com.itheima.mapper.umsMapper.UmsAdminRoleRelationMapper;
import com.itheima.mapper.umsMapper.UmsRoleMapper;
import com.itheima.service.umsService.UmsAdminService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author DogeGuo
 */
@DubboService
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements UmsAdminService {
    @Autowired
    UmsAdminMapper umsAdminMapper;

    @Autowired
    UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Autowired
    UmsRoleMapper umsRoleMapper;

    /**
     * 根据用户名查找用户
     *
     * @param username
     * @return
     */
    @Override
    public UmsAdmin selectByUsername(String username) {
        //创建条件对象
        LambdaQueryWrapper<UmsAdmin> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsAdmin::getUsername, username);

        //调用mapper的查询方法进行查询
        return umsAdminMapper.selectOne(qw);
    }

    /**
     * 根据用户id查询用户数据
     *
     * @param adminId
     * @return
     */
    @Override
    public UmsAdmin selectById(Long adminId) {
        return umsAdminMapper.selectById(adminId);
    }

    /*
     * 分页查询用户信息
     * */
    @Override
    public PageBeanVo findAll(Integer pageNum, Integer pageSize, String keyword) {
        //1.设置分页参数
        Page<UmsAdmin> page = new Page<>(pageNum, pageSize);


        //2.创建条件查询对象-userId
        LambdaQueryWrapper<UmsAdmin> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(keyword),UmsAdmin::getUsername, keyword).or().like(StrUtil.isNotBlank(keyword),UmsAdmin::getNickName, keyword);

        //3.调用mapper.selectPage(..)
        umsAdminMapper.selectPage(page, qw);
        Integer count = umsAdminMapper.selectCount(qw);
        //4.封装pageBeanVo返回
        return new PageBeanVo(pageNum, pageSize, Long.valueOf(count), page.getRecords());
    }

    /*
     * 用户注册
     * */
    @Override
    public UmsAdmin saveUmsAdmin(UmsAdmin umsAdmin) {


        umsAdminMapper.insert(umsAdmin);

        return umsAdminMapper.selectById(umsAdmin);
    }


    /*
     * 删除用户
     * */
    @Override
    public void removeUsmAdmin(Integer id) {
        umsAdminMapper.deleteById(id);
    }

    /*
    * 修改最后登陆时间
    * */
    @Override
    public void updateTime(UmsAdmin umsAdmin) {
        umsAdminMapper.updateById(umsAdmin);
    }

    /*
     * 给用户分配角色
     * */
    @Override
    public void updateRoleById(Long adminId, List<Long> roleIds) {
        // 根据用户的id去查询种间关系表
        LambdaQueryWrapper<UmsAdminRoleRelation> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsAdminRoleRelation::getAdminId,adminId);
        List<UmsAdminRoleRelation> relationList = umsAdminRoleRelationMapper.selectList(qw);
        // 遍历关系集合
        if (CollUtil.isNotEmpty(relationList)) {
            for (UmsAdminRoleRelation roleRelation : relationList) {
                Long roleId = roleRelation.getRoleId();
                UmsRole umsRole = umsRoleMapper.selectById(roleId);
                umsRole.setAdminCount(umsRole.getAdminCount() - 1);
                umsRoleMapper.updateById(umsRole);
            }
            // 统一删除用户id下的关系表
            LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UmsAdminRoleRelation::getAdminId, adminId);
            umsAdminRoleRelationMapper.delete(queryWrapper);
        }

        // 重新添加新的角色
        if (CollUtil.isNotEmpty(roleIds)) {
            for (Long roleId : roleIds) {
                UmsAdminRoleRelation relation = new UmsAdminRoleRelation();
                relation.setAdminId(adminId);
                relation.setRoleId(roleId);
                umsAdminRoleRelationMapper.insert(relation);
                // 更新角色的用户数量
                LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(UmsAdminRoleRelation::getRoleId, roleId);
                Integer count = umsAdminRoleRelationMapper.selectCount(queryWrapper);
                UmsRole umsRole = umsRoleMapper.selectById(roleId);
                umsRole.setAdminCount(count);
                umsRoleMapper.updateById(umsRole);
            }
        }
    }

    //修改用户信息
    @Override
    public void updateUsmAdmin(UmsAdmin umsAdmin) {
        umsAdminMapper.updateById(umsAdmin);
    }
}
